import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { PagedTaxonomyOut, TaxonomyItemDataOut, TaxonomyServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { AppModalComponentBase } from '@shared/common/app-modal-component-base';
import { TaxonomyTreeComponent } from './taxonomy-tree.component';
import { AddTaxonomyItemComponent } from './add-taxonomy-item.component';

@Component({
    selector: 'taxonomy-item-modal',
    templateUrl: './taxonomy-item-modal.component.html'
})
export class TaxonomyItemModalComponent extends AppModalComponentBase {

    @ViewChild('modal', { static: true }) modal: ModalDirective;

    @ViewChild('taxonomyTree', { static: true }) taxonomyTree:TaxonomyTreeComponent;
    @ViewChild('addTaxonomyItem', { static: true }) addTaxonomyItem: AddTaxonomyItemComponent;
    
    @Output() callback: EventEmitter<any> = new EventEmitter<any>();

    title: string;
    typeId: string = null
    constructor(
        injector: Injector,
        private _taxonomyService: TaxonomyServiceProxy
    ) {
        super(injector);
       
    }


    show(data: PagedTaxonomyOut)  {
        this.title = data.localDisplayName;
        this.typeId = data.id;
        this.modal.show();
    }

    close(): void {
        this.modal.hide();
    }

    addingTaxonomyItem(event: any): void {
        this.addTaxonomyItem.taxonomyItemData = new TaxonomyItemDataOut();
        this.addTaxonomyItem.taxonomyItemData.typeId = this.typeId;
        if(event && event.item){
            this.addTaxonomyItem.taxonomyItemData.parentId = event.item.id;
            this.addTaxonomyItem.parentDisplayName = event.item.displayName;
        }
    }

    editTaxonomyItem(event: TaxonomyItemDataOut):void{
        this.addTaxonomyItem.taxonomyItemData = event;
    }

    taxonomyItemCreated(event: TaxonomyItemDataOut):void{
        this.taxonomyTree.taxonomyItemCreated(event);
    }

    taxonomyItemUpdated(event: TaxonomyItemDataOut):void{
        this.taxonomyTree.taxonomyItemUpdated(event);
    }

    reload(){
        this.taxonomyTree.reload();
    }
}
